/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyWidget;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 *
 * @author KF-NRS
 */
public class MyWidgetXml
{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    Document document;
    private Position position;
    private FrontFont frontfont;
    private BgImage bgimage;
    private TrayImage trayimage;
    private final String xmlname = "/ImageFolder/MyWidget.xml";
    private TransformerFactory tf = TransformerFactory.newInstance();
    private Transformer transformer = null;
    private DOMSource source = null;

    public MyWidgetXml(Position position,BgImage bgimage,FrontFont frontfont,TrayImage trayimage)
    {
        this.position=position;
        this.bgimage=bgimage;
        this.frontfont=frontfont;
        this.trayimage=trayimage;

        try
        {
            transformer = tf.newTransformer();
        }
        catch(TransformerConfigurationException ex)
        {
            ex.printStackTrace();
        }
        transformer.setOutputProperty(OutputKeys.ENCODING,"utf-8");
        transformer.setOutputProperty(OutputKeys.INDENT,"yes");

        try
        {
            db = dbf.newDocumentBuilder();
            //document = db.parse(new File(xmlname));
            document = db.parse(this.getClass().getResourceAsStream(this.xmlname));
        }
        catch(SAXException ex)
        {
            ex.printStackTrace();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
        catch(ParserConfigurationException ex)
        {
            ex.printStackTrace();
        }

        getElement();
    }

    public void getElement()
    {
        doPosition();
        doFont();
        doBgimage();
        doTrayImage();
    }

    private void doPosition()
    {
        NodeList nlist = document.getElementsByTagName("position");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;
        int left=0;
        int top=0;
        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("left"))
            {
                try
                {
                    left=Integer.parseInt(cnode.getTextContent().trim());
                }catch(NumberFormatException e)
                {
                    left=0;
                }
                position.setLeft(left);
            }
            else if(cnode.getNodeName().equals("top"))
            {
                try
                {
                    top=Integer.parseInt(cnode.getTextContent().trim());
                }catch(NumberFormatException e)
                {
                    top=0;
                }
                 position.setTop(top);
            }
        }
    }

    private void doFont()
    {
        NodeList nlist = document.getElementsByTagName("font");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;

        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("path"))
            {
                frontfont.setPath(cnode.getTextContent().trim());
            }
            else if(cnode.getNodeName().equals("color"))
            {
                frontfont.setColor(cnode.getTextContent().trim());
            }
        }
    }

    private void doBgimage()
    {
        NodeList nlist = document.getElementsByTagName("bgimage");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;

        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("path"))
            {
                bgimage.setPath(cnode.getTextContent().trim());
            }
        }
    }

    private void doTrayImage()
    {
        NodeList nlist = document.getElementsByTagName("trayimage");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;

        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("path"))
            {
                trayimage.setPath(cnode.getTextContent().trim());
            }
        }
    }

    public void save()
    {
        changePosition();
        changeFont();
        changeBgimage();
        changeTrayImage();
        save_();
    }

    private void changePosition()
    {
        NodeList nlist = document.getElementsByTagName("position");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;

        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("left"))
            {
                cnode.setTextContent(String.valueOf(position.getLeft()));
            }
            else if(cnode.getNodeName().equals("top"))
            {
                cnode.setTextContent(String.valueOf(position.getTop()));
            }
        }
    }

    private void changeFont()
    {
        NodeList nlist = document.getElementsByTagName("font");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;

        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("path"))
            {
                cnode.setTextContent(frontfont.getPath());
            }
            else if(cnode.getNodeName().equals("color"))
            {
                cnode.setTextContent(frontfont.getColorHexString());
            }
        }
    }

    private void changeBgimage()
    {
        NodeList nlist = document.getElementsByTagName("bgimage");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;

        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("path"))
            {
                cnode.setTextContent(bgimage.getPath());
            }
        }
    }

    private void changeTrayImage()
    {
        NodeList nlist = document.getElementsByTagName("trayimage");
        Node node = null;
        if(nlist!=null&&nlist.getLength()>0)
        {
            node = nlist.item(0);
        }

        NodeList clist = node.getChildNodes();
        Node cnode = null;

        for(int i = 0, j = clist.getLength();i<j;i++)
        {
            cnode = clist.item(i);
            if(cnode.getNodeName().equals("path"))
            {
                cnode.setTextContent(trayimage.getPath());
            }
        }
    }

    private void save_()
    {
        PrintWriter pw = null;
        FileOutputStream fos = null;
        StreamResult result = null;
        try
        {
            fos = new FileOutputStream(xmlname);
            pw = new PrintWriter(fos);
            result = new StreamResult(pw);
            source = new DOMSource(document);
            transformer.transform(source,result);
        }
        catch(TransformerConfigurationException e)
        {
            System.out.println(e.getMessage());
        }
        catch(IllegalArgumentException e)
        {
            System.out.println(e.getMessage());
        }
        catch(FileNotFoundException e)
        {
            System.out.println(e.getMessage());
        }
        catch(TransformerException e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            try
            {
                fos.close();

            }
            catch(IOException ex)
            {
                ex.printStackTrace();
            }
            pw.close();
        }
    }
}
